package com.fxbin123.utils;


import com.fxbin123.bean.EmailSenderInfo;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

/**
 * @Author: fxbin
 * @Date: 2018/4/23 21:21
 * @Description:
 */
public class EmailConfig {

    /**
     * 由于有自定义发送方的需求，故采取手动配置的方式，而不是通过yaml 初始化加载邮件发送方配置
     * 中加配置自动实例化JavaMailSenderImpl，本方法只是为了测试使用
     */
    public static JavaMailSenderImpl initJavaMailSender(EmailSenderInfo senderInfo) {


        Properties properties = new Properties();
        properties.setProperty("mail.debug", "true");// 是否显示调试信息(可选)
        properties.setProperty("mail.smtp.starttls.enable", "false");
        properties.setProperty("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        properties.setProperty("mail.smtp.auth", "true");
        properties.put(" mail.smtp.timeout ", " 25000 ");

        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

        javaMailSender.setJavaMailProperties(properties);
        //"smtp.exmail.qq.com"
        javaMailSender.setHost(senderInfo.getEmailHost());
        //"noreplay@zichan360.com"
        javaMailSender.setUsername(senderInfo.getEmailUsername()); // 根据自己的情况,设置username
        //"gWT9yeQpsHPjqZFc"
        javaMailSender.setPassword(senderInfo.getEmailPassword()); // 根据自己的情况, 设置password
        javaMailSender.setPort(senderInfo.getEmailPort()==null?465:senderInfo.getEmailPort());
        javaMailSender.setDefaultEncoding("UTF-8");

        return javaMailSender;
    }

//    public static void main(String[] args) throws Exception {
//        JavaMailSenderImpl sender = initJavaMailSender();
//        String[] ss = { "fanxubin@zichan360.com", "wangzhimin@zichan360.com" };
//
//        sendTextWithHtml(sender, ss, "测试简单文本邮件发送！ ", " <a href='http://www.json.cn/'>test</a>测试简单邮件发送机制！！ ");
//
//        //sendTextWithImg(sender, ss, "测试邮件中嵌套图片!！", "<html><head></head><body><h1>hello 欢迎你!!spring image html mail</h1><img src='cid:image'/></body> , "image", "d:/compare2.png");
//
//        //sendWithAttament(sender,"fanxubin@zichan360.com","测试邮件中上传附件!！","<html><head></head><body><h1>你好：附件中有学习资料！</h1></body></html>","c.png","d:/compare2.png");
//
//        //sendWithAll(sender, "fanxubin@zichan360.com", "测试邮件中嵌套图片!！", "<html><head></head><body><h1>hello 欢迎你!!spring image html ma ", "image", "d:/compare2.png","工作日志.docx","d:/工作日志.docx");
//    }
//
//    public static void sendTextWithHtml(JavaMailSenderImpl sender, String[] tos, String subject, String text)
//            throws Exception {
//        MimeMessage mailMessage = sender.createMimeMessage();
//        initMimeMessageHelper(mailMessage, tos, sender.getUsername(), subject, text);
//        // 发送邮件
//        sender.send(mailMessage);
//
//        System.out.println("邮件发送成功..");
//    }
//
//    private static MimeMessageHelper initMimeMessageHelper(MimeMessage mailMessage, String[] tos, String from,
//                                                           String subject, String text) throws MessagingException {
//        return initMimeMessageHelper(mailMessage, tos, from, subject, text, true, false, "UTF-8");
//    }
//
//
//    private static MimeMessageHelper initMimeMessageHelper(MimeMessage mailMessage, String[] tos, String from,
//                                                           String subject, String text, boolean isHTML, boolean multipart, String encoding) throws MessagingException {
//        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, multipart, encoding);
//        messageHelper.setTo(tos);
//        messageHelper.setFrom(from);
//        messageHelper.setSubject(subject);
//        // true 表示启动HTML格式的邮件
//        messageHelper.setText(text, isHTML);
//
//        return messageHelper;
//    }
//






}    